home *** CD-ROM | disk | FTP | other *** search
- /*
- ** Apple Macintosh Developer Technical Support
- **
- ** Routine demonstrating how to parse Sun's .au sound files.
- **
- ** by Mark Cookson, Apple Developer Technical Support
- **
- ** File: ULAW.c
- **
- ** Copyright ©1996 Apple Computer, Inc.
- ** All rights reserved.
- **
- ** You may incorporate this sample code into your applications without
- ** restriction, though the sample code has been provided "AS IS" and the
- ** responsibility for its operation is 100% yours. However, what you are
- ** not permitted to do is to redistribute the source as "Apple Sample
- ** Code" after having made changes. If you're going to re-distribute the
- ** source, we require that you make it clear in the source that the code
- ** was descended from Apple Sample Code, but that you've made changes.
- */
-
- #include "ULAW.h"
-
- /* I threw this together in about an hour after just looking at the structure
- of some .au files that I made from some known AIFF files. This could use
- some work to get it to deal with formats that I have no idea about, but
- since I have no idea about them, I can't do that...
- */
-
- /*-----------------------------------------------------------------------*/
- OSErr ASoundGetULAWHeader (SoundInfoPtr theSoundInfo,
- long *dataStart,
- long *length)
- /*-----------------------------------------------------------------------*/
- {
- ParamBlockRec pb;
- auHeader ULAWHeader;
- long filePosition = kInit,
- byteCount = kInit,
- sampleSize = kInit,
- tempLength = kInit;
- Fixed sampleRate = kInit;
- OSErr err = noErr;
-
- *dataStart = kInit;
-
- err = SetFPos (theSoundInfo->refNum, fsFromStart, filePosition);
- if (err != noErr) {
- DebugPrint ("\pSetFPos failed!");
- }
- else {
- byteCount = sizeof (auHeader);
- err = FSRead (theSoundInfo->refNum, &byteCount, &ULAWHeader);
- if ((err != noErr) && (err != eofErr)) {
- DebugPrint ("\pFSRead failed!");
- }
- else {
- sampleRate = ASoundLongDoubleToFix (ULAWHeader.sampleRate);
- *dataStart = ULAWHeader.offsetToData;
- *length = ULAWHeader.dataSize;
-
- /* Get the length of the file because SoundEdit stores the uncompressed length of a file
- and not all Sun/NeXT files have the compressed length of the file in the dataSize
- field. We depend on having the right length (the compressed length). */
-
- pb.ioParam.ioCompletion = nil;
- pb.ioParam.ioRefNum = theSoundInfo->refNum;
- err = PBGetEOF (&pb, false);
- err = pb.ioParam.ioResult;
- if (err == noErr) {
- tempLength = (long)pb.ioParam.ioMisc;
- }
- if (*length > tempLength || *length == -1) { /* This would mean that the uncompressed length was stored || no length was stored */
- *length = tempLength - ULAWHeader.offsetToData; /* We want the compressed length */
- }
-
- switch (ULAWHeader.dataFormat) {
- case SND_FORMAT_MULAW_8:
- err = SetupDBHeader (theSoundInfo,
- sampleRate,
- k16BitSample,
- ULAWHeader.numChannels,
- fixedCompression,
- kULawSubType);
- theSoundInfo->needsMasking = false;
- break;
- case SND_FORMAT_LINEAR_8:
- err = SetupDBHeader (theSoundInfo,
- sampleRate,
- k8BitSample,
- ULAWHeader.numChannels,
- notCompressed,
- NoneType);
- theSoundInfo->needsMasking = true;
- break;
- case SND_FORMAT_LINEAR_16:
- err = SetupDBHeader (theSoundInfo,
- sampleRate,
- k16BitSample,
- ULAWHeader.numChannels,
- notCompressed,
- NoneType);
- theSoundInfo->needsMasking = false;
- break;
- default:
- DebugPrint ("\pUnknown or unplayable encoding format");
- err = kUnknownFormat;
- break;
- }
- }
- }
-
- if (err != noErr) {
- DebugPrint ("\pError in ASoundGetULAWHeader");
- }
-
- *length += *dataStart; /* Otherwise we wouldn't read the last few bytes from the end of the sound. */
-
- return err;
- }
-